// 导入需要的config和request

import confing from '../config/config.js';
import request from '../util/request.js';


// 申明请求对象
const measureRequest = {
    // dict请求的基本url
    BASE_URL : confing.MD_URL + "/measure",
    /**
     * 获取所有单位
     */
    getAllMeasure(callback){
        let url = this.BASE_URL + "/getAllMeasure";
        request.get(url,callback)
    },
    /**
     * 保存单位信息
     * @param {*} dict 
     * @param {*} callback 
     */
    save(mdUnitMeasure,callback){
        if(mdUnitMeasure){
            request.post(this.BASE_URL + "/save",mdUnitMeasure,callback);
        }
    },
    /**
     * 多条件分页查询
     * @param {*} page 
     * @param {*} limit 
     * @param {*} measure 
     * @param {*} callbcak 
     */
    list(page = 1,limit = 10,mdUnitMeasure,callbcak){
        // url
        let url = this.BASE_URL + "/list?page=" + page + "&limit=" + limit;
        if(mdUnitMeasure){
            if(mdUnitMeasure.measureCode){
                url += "&measureCode=" + mdUnitMeasure.measureCode;
            }
            if(mdUnitMeasure.measureName){
                url += "&measureName=" + mdUnitMeasure.measureName;
            }
            
        }
        // 发送请求
        request.get(url,callbcak);
    },
    /**
     * 删除字典
     * @param {*} measureId 
     * @param {*} callback 
     */
    delete(measureId,callback){
        let url = this.BASE_URL + "/delete?measureId=" + measureId;
        request.delete(url,callback);
    },
    /**
     * 批量删除字典
     * @param {*} measureIds 
     * @param {*} callback 
     */
    deletes(measureIds,callback){
        let url = this.BASE_URL + "/deletes?t=1";
        measureIds.forEach(element => {
            url += "&measureId=" + element;
        });
        request.delete(url,callback)
    },
    edit(mdUnitMeasure,callback){
        console.log(mdUnitMeasure);
        let url = this.BASE_URL + "/update";
        if(mdUnitMeasure != undefined && mdUnitMeasure.measureId != undefined){
            request.put(url,mdUnitMeasure,callback)
        }
    },
    get(measureId,callback){
        if(measureId){
            // 准备url
            let url = this.BASE_URL + "/get?measureId=" + measureId;
            request.get(url,callback);
        }
        // 如果没有userId，不做任何处理
    }
}

// 将dictRequest暴漏出去
export default measureRequest;